<?php

namespace app\controller;

use app\BaseController;
use app\Request;
use app\service\BigServiceRush;

class IndexpcRush extends BaseController
{

    protected $bigServiec_rush;
    protected $r; //Request 对象

    public function __construct(BigServiceRush $bigServiec_rush, Request $r)
    {
        $this->bigServiec_rush = $bigServiec_rush;
        $this->r = $r;
    }

    //抢购的万能表单
    //  public function getneworder_rush2(Request $request)
    public function findformsetbypidandaid_rush(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr1 = $this->bigServiec_rush->findformsetbypidandaid_rush($pid,$aid);
        $len = count($arr1);
        if($len > 0 ){
            $item = $arr1[0];
            $value = $item['value'];  // mediumtext NOT NUL
            $r['has']= 'yes';
            $r['data']= unserialize($value);
            return json_encode($r);
        }else{
            $r['has'] = 'no';
            $r['msg'] = "没有数据";
            return json_encode($r);
        }

    }

    //v1有点像测试控制器
    public function findthemeinfobypidaid_rush(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr1 = $this->bigServiec_rush->findthemeinfobypidaid_rush($pid,$aid);
        return json_encode($arr1);
    }

    //todo 三大组件A
    //A table
    public function getinfofromtable($pid, $aid)
    {
        $arr1 = $this->bigServiec_rush->findthemeinfobypidaid_rush($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            return $arr1[0];
        }
        return null;
    }

    //B config
    public function getfinofromconfig($pid, $aid)
    {
        $arr1 = $this->bigServiec_rush->findconfigbypidaid_rush($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            $item = $arr1[0];
            //return json_encode($item);
            $value = $item['value'];  // mediumtext NOT NUL   也是value
            return unserialize($value); //
        }
        return null;
    }

    //C config
    public function getfinfofromset($pid, $aid){
        $arr_set = $this->bigServiec_rush->findformsetbypidandaid_rush($pid, $aid);
        $len = count($arr_set);
        if ($len > 0) {
            $item = $arr_set[0];
            $value = $item['value'];  // mediumtext NOT NUL
            return unserialize($value);
        }
        return null;
    }


    public function pcfindbaseinfo_rush(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr = $this->getinfofromtable($pid, $aid);

        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];

        $pcdata = [];
        if (!is_null($arr)) {
            $r['has'] = 'yes';
            $pcdata['title'] = $arr['title'];
            $pcdata['mobile'] = $arr['mobile'];
            $arr2 = $this->getfinofromconfig($pid, $aid);
            if (!is_null($arr2)) {
                if (is_array($arr2)) {
                    $r['data_type2'] = 'php_array2';
                    $pcdata['jubf'] = $arr2['jubf'];  //增补的
                }
            }
            $r['pcdata'] = $pcdata;
            //$arr['jubf22'] ="厉害22"; //写在后面没有用
            return json_encode($r);
        }
        $r['has'] = 'no';
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    public function pcfindshareinfo_rush(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        //要不要翻着来一下 arr1是config  arr2是table
        $arr1 = $this->getfinofromconfig($pid, $aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];
        if (!is_null($arr1)) {
            $r['has'] = 'yes';
            $pcdata['sharetitle1'] = $arr1['sharetitle1'];
            $pcdata['sharetitle2'] = $arr1['sharetitle2'];

            $arr2 = $this->getinfofromtable($pid, $aid);
            if (!is_null($arr2)) {
                if (is_array($arr2)) {
                    $r['data_type2'] = 'php_array2';
                    $pcdata['img'] = $arr2['img'];  //增补的
                    $pcdata['begintime']=$arr2['begintime'];
                    $pcdata['endtime']=$arr2['endtime'];
                    $pcdata['status']=$arr2['status'];
                }
            }
            $r['pcdata'] = $pcdata;
            //$arr['jubf22'] ="厉害22"; //写在后面没有用
            return json_encode($r);
        }
        $r['has'] = 'no';
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    public function pcfindptinfo_rush(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid, $aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        //return json_encode($arr_config);
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            $pcdata['payfee'] = $arr_config['payfee'];  //增补的
            $pcdata['payfee_txt'] = "支付金额";


            $pcdata['limitnum'] = $arr_config['limitnum'];  //增补的
            $pcdata['limitnum_txt'] = "活动总团数";  //增补的
            //todo 用户限制参团数

            $pcdata['attend'] = $arr_config['attend'];  //增补的
            $pcdata['attend_txt'] = "用户限制参团数";  //增补的

//            //合并 headstyle
//            $pcdata['headstyle'] = $arr_config['headstyle'];  //增补的


            $r['pcdata'] = $pcdata;
            return json_encode($r);
        }
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    public function pcfindgroup_rush(Request $request){
        //我知道了
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];

        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            $pcdata['work_group'] = $arr_config['work_group'];  //增补的

            $r['pcdata'] = $pcdata;
            return json_encode($r);
        }
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    public function pcfindredall_rush(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            //todo work
            //$pcdata['headlogo'] = $arr_config['headlogo'];
            $pcdata = $arr_config;

            $r['pcdata'] = $pcdata;

            return json_encode($r);
        }
        return json_encode($r);
    }

    //抢购的文字的按钮-好像只有一个
    public function pcfindbutton_rush(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';

            //$pcdata['lablegroup'] = $arr_config['lablegroup'];   //报错
            $pcdata = $arr_config;



            $arr_set  = $this->getfinfofromset($pid,$aid);
            if (!is_null($arr_set)) {
                $pcdata['wn_set']= $arr_set;
            }else{
                $pcdata['wn_set']= null;
            }

            $r['pcdata'] = $pcdata;


            return json_encode($r);
        }
        return json_encode($r);
    }


}